﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using King.Engine.GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using King.Engine.Core;
using King.Engine.Components;

namespace King.Engine.Tester
{
    public class Player : AnimatedGameObject<PlayerAnimation>
    {

        #region Fields

        private static Dictionary<PlayerAnimation,Animation> _animations = new Dictionary<PlayerAnimation,Animation>()
            {
                {PlayerAnimation.Walk, new Animation("PlayerWalk", 100, 100, 25, 14, 20, .4f)},
                {PlayerAnimation.Attack, new Animation("PlayerAttack", 100, 100, 53, 20, 20, .4f)}
            };

        private Label _label;
        
        #endregion


        #region Properties

        public static int Speed = 3;
        public static bool ShowLabel = true;

        #endregion


        #region Constructors

        static Player()
        {
            var controls = new Dictionary<PlayerControl, Keys>()
            {
                {PlayerControl.Left, Keys.A},
                {PlayerControl.Right, Keys.D},
                {PlayerControl.Up, Keys.W},
                {PlayerControl.Down, Keys.S}
            };

            Input<PlayerControl>.Initialize(controls, PlayerControl.EnableAttack);
        }

        public Player(int x, int y)
            : base(_animations, PlayerAnimation.Walk)
        {
            this.Bounds = new Rectangle(x, y, 100, 100);

            AddComponent<CollisionPusher<Block>>();

            if (ShowLabel)
            {
                _label = new Label("Label", .2f)
                {
                    IsCentered = true,
                    Location = new Vector2(Bounds.Width / 2, -10),
                    Text = "Player"
                };
                AddComponent(_label);
            }
        }

        #endregion


        #region Protected Methods
        
        protected override void UpdateBeforeComponents(GameTime gameTime)
        {
            if (ControlPressed(PlayerControl.Left))
            {
                this.Bounds.Offset(-Speed, 0);
                this.IsHorizontallyFlipped = false;
            }

            if (ControlPressed(PlayerControl.Right))
            {
                this.Bounds.Offset(Speed, 0);
                this.IsHorizontallyFlipped = true;
            }

            if (ControlPressed(PlayerControl.Up))
            {
                this.Bounds.Offset(0, -Speed);
            }

            if (ControlPressed(PlayerControl.Down))
            {
                this.Bounds.Offset(0, Speed);
            }

            if (ControlTapped(PlayerControl.EnableAttack))
            {
                this.SetNonLoopingAnimation(PlayerAnimation.Attack, Walk);
            }

            base.UpdateBeforeComponents(gameTime);
        }

        #endregion


        #region Private Methods

        private void Walk()
        {
            this.SetAnimation(PlayerAnimation.Walk);
        }

        private bool ControlPressed(PlayerControl control)
        {
            return Input<PlayerControl>.ControlPressed(control);
        }

        private bool ControlTapped(PlayerControl control)
        {
            return Input<PlayerControl>.ControlTapped(control);
        }

        #endregion
    }

    public enum PlayerAnimation
    {
        Walk,
        Attack
    }

    public enum PlayerControl
    {
        Left,
        Right,
        Up,
        Down,
        EnableAttack
    }
}
